package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.domain.dao.PaasAppDao;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * paasAppModel业务实现
 * @author kaka
 * @time 2018-7-30 18:07
 */
@Service("paasAppModel")
public class PaasAppModelImpl implements PaasAppModel {

    private static final String CLOUMNS = "id, corpid, name, name_en, icon,color, saas_mark, alias,enable, display, sort, creator_id, add_time, update_time, del ";
    @Resource
    private PaasAppDao paasAppDao;

    @Override
    public Integer insert(PaasAppEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return paasAppDao.insert(entity);
    }

    @Override
    public List<PaasAppEntity> insertBatch(List<PaasAppEntity> list) {
        paasAppDao.insertBatch(list);
        return list;
    }

    @Override
    public List<PaasAppEntity> list(Map<String, Object> map) {
        map.put("columns", CLOUMNS);
        return paasAppDao.findEntitys(map);
    }

    @Override
    public List<PaasAppEntity> findEntitys(Map<String, Object> param) {
        return paasAppDao.findEntitys(param);
    }

    @Override
    public PaasAppEntity getByKey(Long key, String corpid) {
        return paasAppDao.getByKey(key, corpid);
    }

    @Override
    public PaasAppEntity getByAlias(String alias, String corpid) {
        return paasAppDao.getByAlias(alias, corpid);
    }
    
    @Override
    public List<PaasAppEntity> getDingBundleByAlias(Long idGet, String corpid) {
        return paasAppDao.getDingBundleByAlias(idGet, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return paasAppDao.getEntitysCount(map);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 获取开通并开启应用的公司id
     *
     * @param param
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public List<String> findCorps(Map<String, Object> param) {
        return paasAppDao.findCorps(param);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return paasAppDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer update(PaasAppEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return paasAppDao.update(entity);
    }

    @Override
    public Integer updateSort(List<PaasAppEntity> entity, String corpid) {
        return paasAppDao.updateSort(entity, corpid);
    }

    @Override
    public List<PaasAppEntity> userAppList(Map<String, Object> map) {
        map.put("columns", CLOUMNS);
        return paasAppDao.userAppList(map);
    }

    @Override
    public List<String> getCorpidList() {
        return paasAppDao.getCorpidList();
    }

    @Override
    public void updateBatch(List<PaasAppEntity> paasAppEntityList,String corpid){
        paasAppDao.updateBatch(paasAppEntityList,corpid);
    }

    @Override
    public Boolean validateAppEnable(Long appId, String alias, Integer saasMark, String corpid) throws XbbException {
        boolean enable = false;
        PaasAppEntity paasAppEntity;
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark) && Objects.isNull(appId) && StringUtil.isNotEmpty(alias)) {
            paasAppEntity = getByAlias(alias, corpid);
        } else {
            paasAppEntity = getByKey(appId, corpid);
        }
        if (Objects.nonNull(paasAppEntity) && paasAppEntity.getEnable().equals(1)) {
            enable = true;
        }
        return enable;
    }

    @Override
    public List<String> getJxcEnableTime(Map<String,Object> map){
        return paasAppDao.getJxcEnableTime(map);
    }

    @Override
    public List<PaasAppEntity> getNameByKeys(List<Long> appIds, String corpid) {
        return paasAppDao.getNameByKeys(appIds,corpid);
    }

    @Override
    public void validateCallCenter(String corpid, String alias) throws XbbException {
        if (StringUtil.isEmpty(alias)) {
            return;
        }
        if (XbbRefTypeEnum.CALL_CENTER_XBB.getAlias().equals(alias)) {
            PaasAppEntity duYanApp = getByAlias(XbbRefTypeEnum.CALL_CENTER_DU_YAN.getAlias(), corpid);
            if (Objects.nonNull(duYanApp) && Objects.equals(duYanApp.getEnable(), 1)) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260007);
            }
        } else if (XbbRefTypeEnum.CALL_CENTER_DU_YAN.getAlias().equals(alias)) {
            PaasAppEntity callCenter = getByAlias(XbbRefTypeEnum.CALL_CENTER_XBB.getAlias(), corpid);
            if (Objects.nonNull(callCenter) && Objects.equals(callCenter.getEnable(), 1)) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260006);
            }
        }
    }
}
